package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lst.api.constant.DeleteEnum;
import com.lst.api.entity.LoanInfo;
import com.lst.api.entity.TaxInfo;
import com.lst.api.entity.WorkCompanyInfo;
import com.lst.api.exception.RequestParamErrorException;
import com.lst.api.mapper.TaxInfoMapper;
import com.lst.api.service.ITaxInfoService;
import com.lst.api.vo.TaxInfoVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 企业申请税务信息业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:38
 * @version: V1.0
 */
@Service
public class TaxInfoService extends BaseService<TaxInfoMapper, TaxInfo> implements ITaxInfoService {

    @Override
    public boolean saveOrUpdate(long userId, String applyNo, List<TaxInfoVo> taxInfoVoList) {
        if (CollectionUtils.isEmpty(taxInfoVoList)) {
            return false;
        }
        Calendar calendar = Calendar.getInstance();
        String thisYear = String.valueOf(calendar.get(Calendar.YEAR) - 1);
        List<TaxInfo> taxInfoList = new ArrayList<>();
        boolean flag = false;
        for (TaxInfoVo taxInfoVo : taxInfoVoList) {
            if (taxInfoVo.getYear().equals(thisYear)) {
                flag = true;
                taxInfoVo.check();
            }
            TaxInfo taxInfo = new TaxInfo(userId, applyNo, taxInfoVo);
            taxInfoList.add(taxInfo);
        }
        if (!flag) {
            throw new RequestParamErrorException("缺少最新一年的税务等级！");
        }
        UpdateWrapper<TaxInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .eq(TaxInfo.APPLY_NO, applyNo)
                .eq(TaxInfo.PROMO_USER_ID, userId)
                .set(TaxInfo.IS_DELETE, DeleteEnum.DELETE_YES.getKey());
        super.update(updateWrapper);
        return super.saveBatch(taxInfoList);
    }

    @Override
    public List<TaxInfo> listByApplyNo(String applyNo) {
        QueryWrapper<TaxInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(TaxInfo.IS_DELETE, DeleteEnum.DELETE_NO.getKey())
                .eq(TaxInfo.APPLY_NO, applyNo)
                .orderByAsc(TaxInfo.YEAR);
        return super.list(queryWrapper);
    }

    @Override
    public TaxInfo getByApplyNoAndYear(String applyNo, int year) {
        QueryWrapper<TaxInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(TaxInfo.IS_DELETE, DeleteEnum.DELETE_NO.getKey())
                .eq(TaxInfo.APPLY_NO, applyNo)
                .eq(TaxInfo.YEAR, String.valueOf(year))
                .orderByDesc(TaxInfo.CREATE_AT);
        List<TaxInfo> list = super.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
         return list.get(0);
    }
}